package com.xmutca.sso.dao;

import com.xmutca.core.mybatis.MyBatisDao;
import com.xmutca.core.mybatis.lang.MybatisExtendedLanguageDriver;
import com.xmutca.sso.api.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-08-27 22:54
 */
@MyBatisDao
public interface PermissionDao {

    /**
     * 权限新增
     * @param permission
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into t_sys_permission(name, menu, sign, state) values(#{name}, #{menu}, #{sign}, #{state})")
    int add(Permission permission);

    /**
     * 权限状态更新
     * @param id
     * @return
     */
    @Update("update t_sys_permission set state = #{state} where id = #{id}")
    int updateState(@Param("state") Integer state, @Param("id") Integer id);

    /**
     * 权限状态批量更新
     * @param state
     * @param ids
     * @return
     */
    @Update("update t_sys_permission set state = #{state} where id = #{id}")
    int batchUpdateState(@Param("state") Integer state, @Param("ids") List<Integer> ids);

    /**
     * 根据菜单列表批量删除权限
     * @param ids
     * @return
     */
    @Lang(MybatisExtendedLanguageDriver.class)
    @Delete("update t_sys_permission set state = #{state} where menu in (#{ids})")
    int batchUpdateStateByMenu(@Param("state") Integer state, @Param("ids") List<Integer> ids);

    /**
     * 根据菜单列表删除权限
     * @param state
     * @param id
     * @return
     */
    @Delete("update t_sys_permission set state = #{state} where menu = #{id}")
    int updateStateByMenu(@Param("state") Integer state, @Param("ids") Integer id);

    /**
     * 权限更新
     * @param permission
     * @return
     */
    @Update("update t_sys_permission set name = #{name}, menu = #{menu}, sign = #{sign} where id = #{id}")
    int update(Permission permission);

    /**
     * 权限读取
     * @param id
     * @return
     */
    @Select("select * from t_sys_permission where id = #{id}")
    Permission get(Integer id);

    /**
     * 根据菜单id进行查询
     * @param id
     * @return
     */
    @Select("select * from t_sys_permission where menu = #{id}")
    List<Permission> queryByMenu(Integer id);

    /**
     * 查询数据
     * @param paramMap
     * @return
     */
    List<Permission> query(Map<String, Object> paramMap);

    /**
     * 统计查询数据记录数
     * @param paramMap
     * @return
     */
    int countQuery(Map<String, Object> paramMap);

    /**
     * 读取用户所有的权限签名
     * @param userId
     * @return
     */
    List<String> queryPermission(@Param("userId") String userId);
}
